package api

import (
	"demo/models/response"
	"demo/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type DeptController struct{}

var deptService = new(service.DeptService)

// GetAll 获取部门列表
func (d *DeptController) GetAll(c *gin.Context) {
	allDepts := deptService.GetAll()
	response.OkWithDetailed(allDepts, "获取部门列表成功", c)
}

// GetDeptListByPage 分页获取部门列表
func (d *DeptController) GetDeptListByPage(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))

	byPage, err := deptService.GetDeptListByPage(page, size)
	if err != nil {
		response.FailWithMessage("分页查询部门信息失败", c)
		return
	}
	response.OkWithDetailed(byPage, "获取部门列表成功", c)
}

// GetDeptById 根据id获取部门信息
func (d *DeptController) GetDeptById(c *gin.Context) {
	param := c.Param("id")
	dept, err := deptService.GetDeptById(param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	response.OkWithDetailed(dept, "success", c)
}

// DeleteDeptById 根据id删除部门信息
func (d *DeptController) DeleteDeptById(c *gin.Context) {
	err := deptService.DeleteDeptById(c.Param("id"))
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (d *DeptController) DeleteBatchDeptById(c *gin.Context) {
	fmt.Println("批量删除")
	fmt.Println(c.Param("ids"))
	err := deptService.DeleteBatchDeptById(c.Param("ids"))
	if err != nil {
		response.FailWithMessage("批量删除数据失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}
